﻿@if (false) {<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.min.js" type="text/javascript" />}
@{string list = "list4", pager = "pager4", entity = "ProductItem";}
<style type="text/css">
    #orderitemtable{ border-top: solid 1px rgb(170,170,170); border-left: solid 1px rgb(170,170,170);}
    #orderitemtable th{ width: 80px; }
    #orderitemtable th, #orderitemtable td{ border-bottom:solid 1px rgb(170,170,170); border-right:solid 1px rgb(170,170,170) }
    #orderitemtable input{ border:1px; width: 80px; }
    .orderi p{ height: 20px; }
    .orderi span:first-child, #others span{ width: 80px; display: inline-block;}
    #others input{ width: 300px; }
</style>
<table id="@list" class=":first-child" cellpadding="0" cellspacing="0">
</table>
<div id="@pager" class="scroll" style="text-align: center;">
</div>
<div id="orderitem" style="display:none" title="查看采购排产单" >
<div style="width: 340px; float:left" class="orderi">
    <p><span>合同编号：</span><input type="text" id="number" /></p>
    <p><span>供货商：</span><span id="supplier"></span></p>
    <p><span>地址：</span><span id="address"></span></p>
    <p><span>电话：</span><span id="phone"></span></p>
    <p><span>传真：</span><span id="fax"></span></p>
    <p><span>联系人：</span><span id="supplieruser" ></span></p>
</div>
<div style="width: 340px; margin-left: 360px" class="orderi">
    <p><span>日期：</span><input type="text" id="date" /></p>
    <p><span>需方：</span><input type="text" id="demand" /></p>
    <p><span>地址：</span><input type="text" id="daddress" /></p>
    <p><span>电话：</span><input type="text" id="dphone" /></p>
    <p><span>传真：</span><input type="text" id="dfax" /></p>
    <p><span>联系人：</span><input type="text" id="demanduser" /></p>
</div>  
    <table id="orderitemtable" cellspacing="0" cellpadding="0" border="0" class="ui-jqgrid-btable">
        <tr>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width:100px">编号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width:140px">配件名称</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">型号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">数量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单位</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单价</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">总价</th>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width:40px">删除</th>
        </tr>
    </table>
    <div id="others">
    <p><span>备注：</span><input type="text"  id="remark" /></p>
    <p><span>产品需求：</span><input type="text" id="ProductRequirements" /></p>
    <p><span>包装需求：</span><input type="text" id="PackagingRequirements" /></p>
    <p><span>交货日期：</span><input type="text" id="DeliveryDate" /></p>
    <p><span>收货地址：</span><input type="text" id="DeliveryAddress" /></p>
    <p><span>收货人：</span><input type="text" id="Consignee" /></p>
    <p><span>收货人电话：</span><input type="text" id="ConsigneeTelphone" /></p>
    <p><span>其他事项：</span><input type="text" id="Other" /></p>
    </div>
</div>
<script type="text/javascript">
    jQuery(function () {
        $("#@list").jqGrid({
            url: "/@entity/index/",
            colNames: ["物料名称", "物料编号", "物料名称", "规格", "型号", "单位", "供货商名称", "供货商编号", "供货商名称", "备注"],
            colModel: [
            $.jgridx.customAutoCol("ProductId", "/productitem/getproduct", "/product/get?id="),
            $.extend($.jgridx.strCol("Product.Number"), { editable: false }),
            $.extend($.jgridx.strCol("Product.Name"), { editable: false }),
            { name: 'Product.Spec', index: 'Product.Spec', width: 80, search: false },
            { name: 'Product.Model', index: 'Product.Model', width: 80, search: false },
            { name: 'Product.Unit', index: 'Product.Unit', width: 80, search: false },
            $.jgridx.customAutoCol("SupplierId", "/productitem/getsupplier", "/supplier/get?id="),
            $.extend($.jgridx.strCol("Supplier.Number"), { editable: false }),
            $.extend($.jgridx.strCol("Supplier.Name"), { editable: false }),
            {
                name: "Remark",
                index: "Remark",
                width: 280,
                editable: true,
                edittype: "textarea",
                search: false
            }
            ],

            pager: $('#@pager'),
            sortname: "@(entity)Id",
            sortorder: "desc",

            loadComplete: function (data) {

                if (!data.success)
                    $(this).jqGrid("clearGridData", true);
            }
        });
        $("#@list").jqGrid('navGrid', '#@pager',
        {
            edit: true,
            add: true,
            search: true,
            del: true,
            refresh: true
        },
        {
            url: "/@entity/edit/"
        },
        {
            url: "/@entity/add/"
        },
        {
            url: "/@entity/delete/"
        }, {}, {}).jqGrid('navButtonAdd', "@pager", { caption: "",
            buttonicon: "ui-icon ui-icon-circle-plus",
            onClickButton: function () {
                var rowid = $("#@list").jqGrid('getGridParam', 'selarrrow');
                if (rowid == "") {
                    Alert("警告", "请选择一条记录！");
                    return;
                }
                //$.each(rowid, function (index, value) {
                var a = $('#supplieruser').html();
                if ($('#supplieruser').html() == "") {
                    $.getJSON("/productitem/getbyid?id=" + rowid[0], function (data) {
                        var v = data.value;
                        $('#supplier').html(data.supplier);
                        $('#address').html(data.address);
                        $('#phone').html(data.phone);
                        $('#fax').html(data.fax);
                        var st = "<select>";
                        for (var i in v) {
                            st += "<option value='" + v[i].key + "'"
                            //if(i==0){st += "selected='selected'";}
                            st += ">" + v[i].value + "</option>";
                        }
                        st += "</select>";
                        $('#supplieruser').append(st);
                    });
                }

                var rowids = [];
                var items = [];
                $.each(rowid, function (index, value) {
                    var existence = false;
                    $.each($('#orderitem tr'), function () {
                        if ($(this).attr('id') == value) {
                            var c = parseFloat($(this).find('td').eq(3).find('input').val());
                            $(this).find('td').eq(3).find('input').val(c + 1);

                            existence = true;
                        }
                    });
                    if (!existence) {
                        var tds = $('#list4 tr[id=' + value + ']').find('td');
                        var item = '<tr id=' + value + '><td>' + tds.eq(2).html() +
                            '</td><td>' + tds.eq(3).html() +
                            '</td><td>' + tds.eq(4).html() +
                            '</td><td><input type="text" style="text-align: right" value="0" />' +
                            '</td><td>' + tds.eq(6).html() +
                            '</td><td><input type="text" style="text-align: right" value="0" />' +
                            '</td><td style="text-align: right">0</td>' +
                            '<td style="padding-left:12px"><a href="#" class="ui-corner-all delete" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></td></tr>'
                        items.push(item);
                    }
                });
                if (items.length > 0) {
                    $(items.join('')).appendTo('#orderitemtable');
                }
                Alert("恭喜", "添加成功！")
            },
            position: "last",
            title: "添加到采购排产单"
        }).jqGrid('navButtonAdd', "@pager", { caption: "",
            buttonicon: "ui-icon ui-icon-circle-zoomin",
            onClickButton: function () {
                $("#orderitem").dialog("open");
            },
            position: "last",
            title: "查看采购排产单"
        });
    });
    function FloatMul(arg1, arg2) {
        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
        try { m += s1.split(".")[1].length } catch (e) { }
        try { m += s2.split(".")[1].length } catch (e) { }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    }
    $(function () {
        var d = new Date();
        var m = d.getMonth() + 1;
        $('#date').val(d.getFullYear() + '-' + m + '-' + d.getDate());
        $('#date').datepicker();
        $('#DeliveryDate').datepicker();
        $('.delete').live('click', function () {
            $(this).parent().parent().remove();
        });

        $('#orderitemtable input').live('keyup', function () {
            var c = $(this).parent().parent().find("input").eq(0).val();
            var v = $(this).parent().parent().find("input").eq(1).val();

            var total = FloatMul(c, v);

            $(this).parent().parent().find("td").eq(6).html(total);
        });
        $("#orderitem").dialog({
            autoOpen: false,
            height: 580,
            width: 750,
            modal: true,
            buttons: {
                "保存": function () {
                    var data = {
                        'number': $('#number').val(),
                        'supplieruserid': $('#supplieruser select').val(),
                        'date': $('#date').val(),
                        'demand': $('#demand').val(),
                        'address': $('#daddress').val(),
                        'phone': $('#dphone').val(),
                        'fax': $('#dfax').val(),
                        'demanduser': $('#demanduser').val(),
                        'remark': $('#remark').val(),
                        'ProductRequirements': $('#ProductRequirements').val(),
                        'PackagingRequirements': $('#PackagingRequirements').val(),
                        'DeliveryDate': $('#DeliveryDate').val(),
                        'DeliveryAddress': $('#DeliveryAddress').val(),
                        'Consignee': $('#Consignee').val(),
                        'ConsigneeTelphone': $('#ConsigneeTelphone').val(),
                        'Other': $('#Other').val()
                    };
                    //var a = {};
                    var length = $('#orderitemtable tr').length;
                    var ids = [];
                    var cs = []
                    var ps = [];
                    for (var i = 0; i < length - 1; i++) {
                        var id = $('#orderitemtable tr').eq(i + 1).attr('id');
                        ids.push(id);
                        var c = $('#orderitemtable tr').eq(i + 1).find('input').eq(0).val();
                        cs.push(c);
                        var p = $('#orderitemtable tr').eq(i + 1).find('input').eq(1).val();
                        ps.push(p);
                    }
                    $.extend(data, { 'productitemids': ids.join(','), 'counts': cs.join(','), 'prices': ps.join(',') });
                    $.post('/order/add', data, function (value) {
                        if (value.success) {
                            Alert('恭喜', "采购排产单保存成功");
                            $('#supplieruser').html('');
                            $('#supplier').html('');
                            $('#address').html('');
                            $('#phone').html('');
                            $('#fax').html('');
                            $('#orderitem input:text').val('');
                            $('#orderitemtable td').parent().remove();
                            var d = new Date();
                            var m = d.getMonth() + 1;
                            $('#date').val(d.getFullYear() + '-' + m + '-' + d.getDate());
                            $("#orderitem").dialog("close");
                        } else {
                            Alert('错误', "请重试！");
                        }
                    });
                },

                "重置": function () {
                    $('#supplieruser').html('');
                    $('#supplier').html('');
                    $('#address').html('');
                    $('#phone').html('');
                    $('#fax').html('');
                    $('#orderitem input:text').val('');
                    $('#orderitemtable td').parent().remove();
                },
                "取消": function () {
                    $(this).dialog("close");
                }
            }
        });
    });
</script>
